/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.zhpan.bannerviewpager.adapter;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.PageSlider;
import ohos.agp.components.RadioContainer;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.audio.SoundPlayer;

import com.example.zhpan.bannerviewpager.ResourceTable;
import com.example.zhpan.bannerviewpager.slice.MainBannerAbilitySlice;
import com.example.zhpan.bannerviewpager.slice.SecondAbilitySlice;

import com.zhpan.bannerview.Banner;
import com.zhpan.bannerview.Banner1;
import com.zhpan.bannerview.BannerHorizontal;
import com.zhpan.bannerview.Log;
import com.zhpan.bannerview.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * ViewCreateHelper
 *
 * @since 2021-03-24
 */
public final class ViewCreateHelper {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "ViewCreateHelper");
    private static final int THREE = 3;
    private static final int ONE = 1;
    private static final int TWO = 2;
    private static final int FOUR = 4;
    private static final int SIXTY = 60;
    private static final int FIVE = 5;
    private static final int TWOZEROZEROZERO = 2000;
    private static final int TWOZERO = 6;
    private Context slice;
    private HashMap<String, ListAdapter> cache;
    private int bannerStyle = 1;
    private MainBannerAbilitySlice mainBannerAbilitySlice;
    private Component component = null;

    /**
     * ViewCreateHelper
     *
     * @param abilitySlice 參數
     * @param mainBannerAbilitySlice 參數
     */
    public ViewCreateHelper(Context abilitySlice, MainBannerAbilitySlice mainBannerAbilitySlice) {
        this.slice = abilitySlice;
        this.mainBannerAbilitySlice = mainBannerAbilitySlice;
        cache = new HashMap<>();
    }

    /**
     * createViewHome
     *
     * @param title 參數
     * @return rootLayout
     */
    public Component createViewHome(String title) {
        Component mainComponent =
            LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_pager_item, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return component;
        }
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        initView(mainComponent, title);
        return rootLayout;
    }

    /**
     * createViewPage
     *
     * @param title 參數
     * @return rootLayout
     */
    public Component createViewPage(String title) {
        Component mainComponent =
            LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_pager_item1, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return component;
        }
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        initView1(mainComponent, title);
        return rootLayout;
    }

    /**
     * createViewPage
     *
     * @param title 參數
     * @return rootLayout
     */
    public Component createViewIndicator(String title) {
        Component mainComponent =
            LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_pager_item2, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return component;
        }
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        initView2(mainComponent, title);
        return rootLayout;
    }

    /**
     * createViewPage
     *
     * @param title 參數
     * @return rootLayout
     */
    public Component createViewOthers(String title) {
        Component mainComponent =
            LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_pager_item3, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return component;
        }
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        initView3(mainComponent, title);
        return rootLayout;
    }

    /**
     * Home 页初始化及效果
     *
     * @param mainComponent 參數
     * @param title 參數
     */
    public void initView(Component mainComponent, String title) {
        ListContainer listContainer = (ListContainer) mainComponent.findComponentById(ResourceTable.Id_list_main); // 列表
        ListAdapter<NewsInfo> listAdapter = new ListAdapter<NewsInfo>(slice,
            ResourceTable.Layout_item_news_layout, getData(0, title)) {
            @Override
            public void convert(ViewHolder viewHolder, NewsInfo item, int position) {
                if (position == 0) {
                    viewHolder.getView(ResourceTable.Id_banner).setVisibility(Component.VISIBLE);
                    viewHolder.getView(ResourceTable.Id_bannerHorizontal).setVisibility(Component.HIDE);
                    viewHolder.getView(ResourceTable.Id_direction).setVisibility(Component.HIDE);
                    try {
                        ((Banner1) viewHolder.getView(ResourceTable.Id_banner))
                            .setImages(getData1()).setBannerTitles(getData2()).setScaleType(THREE)
                            .setDelayTime(TWOZEROZEROZERO).setBannerStyle(FIVE).setTitleTextSize(SIXTY).start();
                    } catch (NotExistException e) {
                        Log.debug(e.getMessage());
                    } catch (WrongTypeException e) {
                        Log.debug(e.getMessage());
                    } catch (IOException e) {
                        Log.debug(e.getMessage());
                    }
                    ((Banner1) viewHolder.getView(ResourceTable.Id_banner)).setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void onBannerClick(int position) {
                        }
                    });
                } else if (position == 1) {
                    viewHolder.getView(ResourceTable.Id_banner).setVisibility(Component.HIDE);
                    viewHolder.getView(ResourceTable.Id_bannerHorizontal).setVisibility(Component.VISIBLE);
                    viewHolder.getView(ResourceTable.Id_direction).setVisibility(Component.HIDE);
                    try {
                        ((BannerHorizontal) viewHolder.getView(ResourceTable.Id_bannerHorizontal))
                            .setImages(getData3()).setBannerTitles(getData2()).setScaleType(THREE)
                            .setDelayTime(TWOZEROZEROZERO).setBannerStyle(FIVE).start1();
                    } catch (NotExistException e) {
                        Log.debug(e.getMessage());
                    } catch (WrongTypeException e) {
                        Log.debug(e.getMessage());
                    } catch (IOException e) {
                        Log.debug(e.getMessage());
                    }
                } else {
                    initViewHolder(viewHolder, item);
                }
            }
        };
        setOnItemClick(listAdapter, listContainer, title);
    }

    /**
     * initViewHolder
     *
     * @param viewHolder 參數
     * @param item 參數
     */
    public void initViewHolder(ViewHolder viewHolder, NewsInfo item) {
        viewHolder.getView(ResourceTable.Id_banner).setVisibility(Component.HIDE);
        viewHolder.getView(ResourceTable.Id_bannerHorizontal).setVisibility(Component.HIDE);
        viewHolder.getView(ResourceTable.Id_direction).setVisibility(Component.VISIBLE);
        viewHolder.setText(ResourceTable.Id_item_news_title, item.getTitle());
        viewHolder.setText(ResourceTable.Id_item_news_desc, item.getDescription());
    }

    /**
     * setOnItemClick
     *
     * @param listAdapter 參數
     * @param listContainer 參數
     * @param title 參數
     */
    public void setOnItemClick(ListAdapter listAdapter, ListContainer listContainer, String title) {
        listContainer.setItemProvider(listAdapter);
        listContainer.addItemVisibilityChangedListener(new ListContainer.ItemVisibilityChangedListener() {
            @Override
            public void onItemAdded(Component component1, int i1) {
            }

            @Override
            public void onItemRemoved(Component component1, int i1) {
            }
        });
        listContainer.setItemClickedListener((list, component1, i1, l1) -> {
            if (i1 == 0) {
                mainBannerAbilitySlice.present(new SecondAbilitySlice(), new Intent());
            }
        });

        cache.put(title, listAdapter);
    }

    /**
     * Page 页初始化及效果
     *
     * @param mainComponent 參數
     * @param title 參數
     */
    public void initView1(Component mainComponent, String title) {
        Banner banner = (Banner) mainComponent.findComponentById(ResourceTable.Id_banner); // banner
        try {
            banner.setImages(getData3())
                .setScaleType(THREE)
                .setDelayTime(TWOZEROZEROZERO)
                .setBannerStyle(1).start1();
        } catch (NotExistException e) {
            Log.debug(e.getMessage());
        } catch (WrongTypeException e) {
            Log.debug(e.getMessage());
        } catch (IOException e) {
            Log.debug(e.getMessage());
        }
        RadioContainer radioContainer = (RadioContainer) mainComponent
            .findComponentById(ResourceTable.Id_radioContainer);
        radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int i3) {
                switch (i3) {
                    case 0:
                        break;
                    case ONE:
                        banner.setBannerStyle(TWO);
                        break;
                    case TWO:
                        break;
                    case THREE:
                        banner.setOnPageChangeListener(new PageSlider.PageChangedListener() {
                            @Override
                            public void onPageSliding(int i1, float v1, int i2) {
                            }

                            @Override
                            public void onPageSlideStateChanged(int i1) {
                            }

                            @Override
                            public void onPageChosen(int i4) {
                                setSound();
                            }
                        });
                        break;
                    case FOUR:
                        break;
                    default:
                        break;
                }
            }
        });
        radioContainer.mark(0);
    }

    /**
     * Indicator 页初始化及效果
     *
     * @param mainComponent 參數
     * @param title 參數
     */
    public void initView2(Component mainComponent, String title) {
        Banner1 indicatorBanner = (Banner1) mainComponent.findComponentById(ResourceTable.Id_banner); // 列表
        try {
            indicatorBanner.setImages(getData3()).setScaleType(THREE).setDelayTime(TWOZEROZEROZERO)
                .setBannerStyle(ONE).setTitleTextSize(SIXTY).start1(THREE);
        } catch (NotExistException e) {
            Log.debug(e.getMessage());
        } catch (WrongTypeException e) {
            Log.debug(e.getMessage());
        } catch (IOException e) {
            Log.debug(e.getMessage());
        }
        RadioContainer radioContainer = (RadioContainer) mainComponent
            .findComponentById(ResourceTable.Id_radioContainer);
        radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int i1) {
                switch (i1) {
                    case 0:
                        indicatorBanner.setIndicatorStyle(0);
                        break;
                    case 1:
                        indicatorBanner.setIndicatorStyle(1);
                        break;
                    case TWO:
                        indicatorBanner.setIndicatorStyle(TWO);
                        break;
                    default:
                        break;
                }
            }
        });
        radioContainer.mark(0);
        RadioContainer radioContainer1 = (RadioContainer) mainComponent
            .findComponentById(ResourceTable.Id_radioContainer1);
        setMarkOnclick(radioContainer1,indicatorBanner);
    }

    /**
     * setMarkOnclick
     *
     * @param radioContainer1 參數
     * @param indicatorBanner 參數
     */
    public void setMarkOnclick(RadioContainer radioContainer1,Banner1 indicatorBanner) {
        radioContainer1.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int i1) {
                switch (i1) {
                    case 0:
                        indicatorBanner.setSlideMode(0);
                        break;
                    case 1:
                        indicatorBanner.setSlideMode(1);
                        break;
                    case TWO:
                        indicatorBanner.setSlideMode(TWO);
                        break;
                    case THREE:
                        indicatorBanner.setSlideMode(THREE);
                        break;
                    case FOUR:
                        indicatorBanner.setSlideMode(FOUR);
                        break;
                    default:
                        break;
                }
            }
        });
        radioContainer1.mark(0);
    }

    /**
     * Others 页初始化及效果
     *
     * @param mainComponent 參數
     * @param title 參數
     */
    public void initView3(Component mainComponent, String title) {
        Banner1 othersBanner = (Banner1) mainComponent.findComponentById(ResourceTable.Id_banner);
        try {
            othersBanner.setImages(getData3()).setScaleType(THREE).setDelayTime(TWOZEROZEROZERO)
                .setBannerStyle(ONE).setTitleTextSize(SIXTY).start1(THREE);
        } catch (NotExistException e) {
            Log.debug(e.getMessage());
        } catch (WrongTypeException e) {
            Log.debug(e.getMessage());
        } catch (IOException e) {
            Log.debug(e.getMessage());
        }
        RadioContainer radioContainer = (RadioContainer) mainComponent
            .findComponentById(ResourceTable.Id_radioContainer);
        radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int i1) {
                if (i1 == 0) {
                    othersBanner.setIndicatorInsideBelow();
                } else if (i1 == 1) {
                    othersBanner.setIndictorFigure();
                } else if (i1 == TWO) {
                    othersBanner.setIndicatorInsideDrawable();
                }
            }
        });
        radioContainer.mark(0);
        jumpOnClick(mainComponent,othersBanner);
        addOnClick(mainComponent,othersBanner);
        insertOnClick(mainComponent,othersBanner);
        Button removeButton = (Button) mainComponent.findComponentById(ResourceTable.Id_removeButton);
        removeButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component1) {
                othersBanner.stopAutoPlay();
                try {
                    othersBanner.deleteUpdateImage();
                } catch (NotExistException e) {
                    Log.debug(e.getMessage());
                } catch (WrongTypeException e) {
                    Log.debug(e.getMessage());
                } catch (IOException e) {
                    Log.debug(e.getMessage());
                }
                othersBanner.startAutoPlay();
            }
        });
    }

    /**
     * jumpOnClick
     *
     * @param mainComponent 參數
     * @param othersBanner 參數
     */
    private void jumpOnClick(Component mainComponent,Banner1 othersBanner) {
        Button jumpButton = (Button) mainComponent.findComponentById(ResourceTable.Id_jumpButton);
        jumpButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component1) {
                new ToastDialog(slice)
                    .setText("Jump to postion: " + othersBanner.jumpRadom())
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .show(); // 跳到0号位重新滑动
            }
        });
    }

    /**
     * addOnClick
     *
     * @param mainComponent 參數
     * @param othersBanner 參數
     */
    private void addOnClick(Component mainComponent,Banner1 othersBanner) {
        Button addButton = (Button) mainComponent.findComponentById(ResourceTable.Id_addButton);
        addButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component1) {
                try {
                    othersBanner.addThreeImage(getData4());
                } catch (NotExistException e) {
                    Log.debug(e.getMessage());
                } catch (WrongTypeException e) {
                    Log.debug(e.getMessage());
                } catch (IOException e) {
                    Log.debug(e.getMessage());
                }
                new ToastDialog(slice)
                    .setText("size = " + othersBanner.getIamgeNum())
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .show(); // 增加三张图
            }
        });
    }

    /**
     * insertOnClick
     *
     * @param mainComponent 參數
     * @param othersBanner 參數
     */
    private void insertOnClick(Component mainComponent,Banner1 othersBanner) {
        Button insertButton = (Button) mainComponent.findComponentById(ResourceTable.Id_insertButton);
        insertButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component1) {
                try {
                    othersBanner.addThreeImage(getData5());
                } catch (NotExistException e) {
                    Log.debug(e.getMessage());
                } catch (WrongTypeException e) {
                    Log.debug(e.getMessage());
                } catch (IOException e) {
                    Log.debug(e.getMessage());
                }
            }
        });
    }

    /**
     * 插值数据
     *
     * @param tab 參數
     * @param title 參數
     * @return curData
     */
    public List<NewsInfo> getData(int tab, String title) {
        List<NewsInfo> curData = new ArrayList<>();
        NewsInfo info1 = new NewsInfo();
        info1.setTitle("标题：" + title);
        info1.setDescription("我是描述啦" + title + Math.random());
        curData.add(info1);
        curData.add(info1);
        for (int i1 = 0; i1 < TWOZERO; i1++) {
            NewsInfo info = new NewsInfo();
            info.setTitle("标题：" + title + i1);
            info.setDescription("我是描述啦" + title + Math.random());
            curData.add(info);
        }
        return curData;
    }

    /**
     * getData1
     *
     * @return list
     */
    public List<Integer> getData1() {
        List<Integer> list = new ArrayList<>();
        list.add(ResourceTable.Media_1);
        list.add(ResourceTable.Media_2);
        list.add(ResourceTable.Media_3);
        list.add(ResourceTable.Media_4);
        list.add(ResourceTable.Media_5);
        return list;
    }

    /**
     * getData2
     *
     * @return list
     */
    public List<String> getData2() {
        List<String> title = new ArrayList<>();
        title.add("边看边买");
        title.add("标题1");
        title.add("园您星梦");
        title.add("拳击");
        title.add("红色法拉利大促销");
        return title;
    }

    /**
     * getData3
     *
     * @return list
     */
    public List<Integer> getData3() {
        List<Integer> list1 = new ArrayList<>();
        list1.add(ResourceTable.Media_advertise0);
        list1.add(ResourceTable.Media_advertise1);
        list1.add(ResourceTable.Media_advertise2);
        list1.add(ResourceTable.Media_advertise3);
        list1.add(ResourceTable.Media_advertise4);
        return list1;
    }

    /**
     * getData4
     *
     * @return list
     */
    public List<Integer> getData4() {
        List<Integer> list1 = new ArrayList<>();
        list1.add(ResourceTable.Media_advertise2);
        list1.add(ResourceTable.Media_advertise3);
        list1.add(ResourceTable.Media_advertise4);
        return list1;
    }

    /**
     * getData5
     *
     * @return list
     */
    public List<Integer> getData5() {
        List<Integer> list1 = new ArrayList<>();
        list1.add(ResourceTable.Media_advertise0);
        return list1;
    }

    /**
     * setSound
     */
    public void setSound() {
        SoundPlayer soundPlayer = new SoundPlayer("packageName"); // 翻页声音设置
        soundPlayer.playSound(SoundPlayer.SoundType.KEYPRESS_DELETE, TWO);
    }
}
